//
//  BDHomeCellMerchantsView.m
//  BadBoy
//
//  Created by wen on 2017/3/13.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDHomeCellMerchantsView.h"
#import "BDHomeModel.h"

@interface BDHomeCellMerchantsView ()

@property (nonatomic, strong) UIView *bkView;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) YYLabel *label;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UIImageView *freeImageView;

@end

@implementation BDHomeCellMerchantsView

- (instancetype)init{
    if (self = [super init]) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGR)];
        [self addGestureRecognizer:tap];
    }
    return self;
}

- (void)tapGR{
    if (self.merchantsBlock) {
        self.merchantsBlock();
    }
}

- (void)configeWithModel:(id)model forForward:(BOOL)forward{
    if (forward == YES) {
        BDHomeModel *homeModel = model;
        NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
        //获取app中所有icon名字数组
        NSArray *iconsArr = infoDict[@"CFBundleIcons"][@"CFBundlePrimaryIcon"][@"CFBundleIconFiles"];
        //取最后一个icon的名字
        NSString *iconLastName = [iconsArr lastObject];
        NSString *imageName = homeModel.images.count ? homeModel.images.firstObject : @"business_pic";
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:imageName] placeholderImage:[UIImage imageNamed:iconLastName]];
        
        NSString *str1 = [@"@" stringByAppendingString:homeModel.user.nickname];
        NSString *str = [str1 stringByAppendingString:[NSString stringWithFormat:@"\n%@", homeModel.post]];
        self.label.font = [UIFont systemFontOfSize:13];
        self.label.textColor = [UIColor colorWithHexString:@"#4c4c4c"];
        NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString: str];
        //设置整段文本size
        one.lineSpacing = 2;
        NSRange range2 = [str rangeOfString:str1];
        //标记文字点击事件
        [one setTextHighlightRange:range2 color:[UIColor colorWithHexString:@"#101010"] backgroundColor:nil tapAction:nil];
        [one setFont:[UIFont systemFontOfSize:17] range:range2];
        
        self.label.attributedText = one;
        if (homeModel.free == YES) {
            self.button.hidden = YES;
            self.freeImageView.hidden = NO;
        }else{
            self.freeImageView.hidden = YES;
            self.button.hidden = NO;
            self.button.selected = homeModel.isPurchase;
            self.button.backgroundColor = homeModel.isPurchase ? [UIColor colorWithHexString:@"#cccccc"] : [UIColor colorWithHexString:@"14cd67"];
        }
        self.bkView.backgroundColor = homeModel.free ? [UIColor colorWithHexString:@"#f7f7f7"] : [UIColor colorWithHexString:@"#eeeeee"];
    }
}

- (void)configeWithModel:(id)model{
    BDHomeModel *homeModel = model;
    NSString *labelStr = @"";
    //首先是要确定是转帖收费还是原贴收费
    //如果收费，那么肯定是，如果不收费那么就判断shop
    BDHomeModel *curModel = homeModel.repost;
    if (homeModel.price != 0 || ([homeModel.shop isNotEmpty] || [homeModel.address isNotEmpty] || [homeModel.contact isNotEmpty])) {
        curModel = homeModel;
    }else {
        curModel = homeModel.repost;
    }
//    if (curModel.status == 1 || curModel.isFreez == YES) {
//        labelStr = ASLocalizedString(@"该主题已删除，无法查看");
//        NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString: labelStr];
//        //设置整段文本size
//        one.yy_font = [UIFont systemFontOfSize:14];
//        one.yy_lineSpacing = 4;
//        self.label.attributedText = one;
//    }else{
        if (curModel.price == 0 || curModel.isPurchase == YES) {
            if ([curModel.shop isNotEmpty]) {
                labelStr = [labelStr stringByAppendingString:[NSString stringWithFormat:ASLocalizedString(@"商家店名：%@"), curModel.shop]];
            }
            if ([curModel.address isNotEmpty]) {
                if ([labelStr isEqualToString:@""]) {
                    labelStr = [labelStr stringByAppendingString:[NSString stringWithFormat:ASLocalizedString(@"商家地址：%@"), curModel.address]];
                }else {
                    labelStr = [labelStr stringByAppendingString:[NSString stringWithFormat:ASLocalizedString(@"\n商家地址：%@"), curModel.address]];
                }
            }
            if ([curModel.contact isNotEmpty]) {
                if ([labelStr isEqualToString:@""]) {
                    labelStr = [labelStr stringByAppendingString:[NSString stringWithFormat:ASLocalizedString(@"联系方式：%@"), curModel.contact]];
                }else {
                    labelStr = [labelStr stringByAppendingString:[NSString stringWithFormat:ASLocalizedString(@"\n联系方式：%@"), curModel.contact]];
                }
            }
            
            self.label.lineBreakMode = NSLineBreakByClipping;
            self.label.font = [UIFont systemFontOfSize:14];
            self.label.numberOfLines = 3;
            NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString: labelStr];
            //设置整段文本size
            one.font = [UIFont systemFontOfSize:14];
            one.lineSpacing = 4;
            self.label.attributedText = one;
        }else{
            self.label.lineBreakMode = NSLineBreakByWordWrapping;
            self.label.textColor = [UIColor colorWithHexString:@"#4c4c4c"];
            BOOL isControl = [[[NSUserDefaults standardUserDefaults] objectForKey:@"versionControl"] boolValue];
            BDUserModel *currentUser = [BDUserModel getCurrentUser];
            
            if (isControl == YES || currentUser.is_auth == NO){
                labelStr = ASLocalizedString(@"主题内容已加密，点击查看解锁方式");
            }else{
                labelStr = [NSString stringWithFormat:ASLocalizedString(@"查看商家联系方式需向作者支付 %.2f元 才能浏览 点击查看"), curModel.price];
            }
            NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString: labelStr];
            //设置整段文本size
            one.font = [UIFont systemFontOfSize:14];
            one.lineSpacing = 4;
            // 获得range， 只设置标记文字的size、下划线
            NSRange range = [labelStr rangeOfString:[NSString stringWithFormat:ASLocalizedString(@" %.2f元 "), homeModel.price]];
            //标记文字点击事件[UIColor colorWithHexString:@"#ff9300"]
            [one setTextHighlightRange:range color:[UIColor orangeColor] backgroundColor:nil tapAction:nil];
            
            NSRange range2 = [labelStr rangeOfString:ASLocalizedString(@"点击查看")];
            //标记文字点击事件
            [one setTextHighlightRange:range2 color:KBDColorBlue backgroundColor:nil tapAction:nil];
            self.label.attributedText = one;
        }
    
    NSString *imageName = homeModel.repost != nil ? homeModel.repost.images.firstObject : homeModel.images.firstObject;
        
//    self.label.text = labelStr;
    NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
    //获取app中所有icon名字数组
    NSArray *iconsArr = infoDict[@"CFBundleIcons"][@"CFBundlePrimaryIcon"][@"CFBundleIconFiles"];
    //取最后一个icon的名字
    NSString *iconLastName = [iconsArr lastObject];
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:imageName] placeholderImage:[UIImage imageNamed:iconLastName]];
    
    if (curModel.price == 0) {
        self.button.hidden = YES;
        self.freeImageView.hidden = NO;
    }else{
        self.freeImageView.hidden = YES;
        self.button.hidden = NO;
        self.button.selected = curModel.isPurchase;
        self.button.backgroundColor = curModel.isPurchase ? [UIColor colorWithHexString:@"#cccccc"] : [UIColor colorWithHexString:@"14cd67"];
    }
    self.bkView.backgroundColor = curModel.price == 0 ? [UIColor colorWithHexString:@"#f7f7f7"] : [UIColor colorWithHexString:@"#eeeeee"];
    [self layoutSubviews];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.bkView.size = CGSizeMake(self.width - 20, self.height - 10);
    self.bkView.left = 10;
    self.bkView.centerY = self.height / 2.0;
    
    self.imageView.size= CGSizeMake(self.bkView.height - 20, self.bkView.height - 20);
    self.imageView.left = self.bkView.left + 6;
    self.imageView.centerY = self.bkView.centerY;
    
    self.label.textAlignment = NSTextAlignmentCenter;
    self.label.textVerticalAlignment = YYTextVerticalAlignmentCenter;
    self.label.numberOfLines = 0;
    self.label.textAlignment = NSTextAlignmentLeft;
//    self.label.font = [UIFont systemFontOfSize:15];
    self.label.backgroundColor = [UIColor clearColor];
    self.label.width = self.width - self.imageView.right - 10 - 40;
    self.label.left = self.imageView.right + 10;
    self.label.height = self.imageView.height;
    self.label.centerY = self.imageView.centerY;
//    self.label.backgroundColor = [UIColor redColor];
//    [self.label sizeToFit];
//    NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString: @"1\n2\n3"];
//    //设置整段文本size
//    one.yy_font = [UIFont systemFontOfSize:14];
//    one.yy_lineSpacing = 4;
//    YYTextLayout *layout = [YYTextLayout layoutWithContainer:[YYTextContainer containerWithSize:CGSizeMake(self.width - self.imageView.right - 10 - 40, MAXFLOAT)] text:one];
//    // BDLog(ASLocalizedString(@"计算后的图高度为：%.2f"), layout.textBoundingSize.height);
    
    
    self.button.size = CGSizeMake(20, self.bkView.height);
    self.button.right = self.bkView.right;
    self.button.centerY = self.bkView.centerY;
    
    self.freeImageView.right = self.bkView.right;
    self.freeImageView.top = self.bkView.top;
}

- (UIView *)bkView{
    if (!_bkView) {
        self.bkView = [[UIView alloc] init];
        [self insertSubview:_bkView atIndex:0];
    }
    return _bkView;
}

- (UIImageView *)imageView{
    if (!_imageView) {
        self.imageView = [[UIImageView alloc] init];
        [self addSubview:_imageView];
    }
    return _imageView;
}

- (YYLabel *)label{
    if (!_label) {
        self.label = [[YYLabel alloc] init];
        [self addSubview:_label];
    }
    return _label;
}

- (UIButton *)button{
    if (!_button) {
        self.button = [UIButton buttonWithType:UIButtonTypeCustom];
        [_button setImage:[UIImage imageNamed:@"lock"] forState:UIControlStateNormal];
        [_button setImage:[UIImage imageNamed:@"lock_on"] forState:UIControlStateSelected];
        [self addSubview:_button];
    }
    return _button;
}

- (UIImageView *)freeImageView{
    if (!_freeImageView) {
        self.freeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 34)];
        _freeImageView.image = [UIImage imageNamed:@"free_icon"];
        [self addSubview:_freeImageView];
    }
    return _freeImageView;
}
@end
